﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FolderMeth
{
    public partial class FormMain : Form
    {
        private string DPATH = "";
        private string DINI = "desktop.ini";
        private string[] SS ;
        public FormMain(string arg)
        {
            InitializeComponent();
            DPATH = arg;
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            //目录下是否存在desktop.ini
            if (File.Exists(Path.Combine(DPATH, DINI)))
            {
                SS = File.ReadAllLines(Path.Combine(DPATH, DINI),Encoding.Default);
            }
            //检测是否存在备注
            if (SS != null)
            {
                for (int i = 0; i < SS.Length; i++)
                {
                    if (SS[i].ToString().StartsWith("InfoTip"))
                    {
                        TB_Remark.Text = SS[i].ToString().Split('=')[1];
                    }
                }
            }
            
        }
        private void BT_DelRemark_Click(object sender, EventArgs e)
        {
            if (File.Exists(Path.Combine(DPATH, DINI)))
            {
                if (MessageBox.Show("此操作会删除目录下的desktop.ini文件，是否继续？", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    File.Delete(Path.Combine(DPATH, DINI));
                }
            }
        }
        private void BT_Save_Click(object sender, EventArgs e)
        {
            string tmp;
            if (SS != null)
            {
               tmp = SS.FirstOrDefault(t => t.StartsWith("InfoTip"));
            }
            else
            {
                tmp = "";
            }
             
            if(tmp==null || tmp == "")
            {
                SS = new string[] { "[.ShellClassInfo]", $"InfoTip={TB_Remark.Text}" };
            }
            else
            {
                for (int i = 0; i < SS.Length; i++)
                {
                    if (SS[i].StartsWith("InfoTip"))
                    {
                        SS[i] = $"InfoTip={TB_Remark.Text}";
                    }
                }
            }
            StreamWriter SW = new StreamWriter(Path.Combine(DPATH, DINI), false, Encoding.Default);
            for (int i = 0; i < SS.Length; i++)
            {
                SW.WriteLine(SS[i]);
            }
            SW.Close();
            File.SetAttributes(DPATH, File.GetAttributes(DPATH) | FileAttributes.ReadOnly);
            this.Close();
        }

        private void BT_Cannel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
